package com.dony.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.SecurityUtils;
import com.dony.shop.domain.ShopeeMerchant;
import com.dony.shop.domain.ShopeeShop;
import com.dony.shop.mapper.ShopeeShopMapper;
import com.dony.shop.service.IShopeeMerchantService;
import com.dony.shop.service.IShopeeShopService;
import com.dony.shop.util.ShopeeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service("shopeeShopService")
public class ShopeeShopServiceImpl extends ServiceImpl<ShopeeShopMapper, ShopeeShop> implements IShopeeShopService {

    @Autowired
    ShopeeUtil shopeeUtil;
    @Autowired
    IShopeeMerchantService merchantService;


    @Override
    public List<ShopeeShop> getPublishShopListByMerchant(Long merchantId) {
        QueryWrapper<ShopeeShop> wrapper = new QueryWrapper<>();
        wrapper.eq("merchant_id", merchantId).eq("is_publish", true).orderByDesc("is_sip");
        //查询商户下面可以发布的店铺
        return this.list(wrapper);
    }


    @Override
    public ShopeeShop getByShopId(Long shopId) {
        return getOne(new QueryWrapper<ShopeeShop>().eq("shop_id", shopId));
    }

    @Override
    public void updateToken(ShopeeShop shop) {
        ShopeeShop dbShop = this.getByShopId(shop.getShopId());
        if (dbShop != null) {
            dbShop.setLastRefreshToken(shop.getLastRefreshToken());
            updateById(dbShop);
        } else {
            log.error("错误：店铺不存在！店铺ID：{}", dbShop.getShopId());
        }
    }

    @Override
    public String selectLastRefreshToken(Long shopId) {
        ShopeeShop shop = this.getByShopId(shopId);
        return shop.getLastRefreshToken();
    }

    @Override
    public void setAuth(Long shopId, boolean auth) {
        UpdateWrapper<ShopeeShop> wrapper = new UpdateWrapper<>();
        wrapper.set("auth", auth).eq("shop_id", shopId);
        this.update(wrapper);
    }

    @Override
    public List<ShopeeShop> listByParam(ShopeeShop shop) {
        if (shop == null) {
            return this.list();
        }
        QueryWrapper<ShopeeShop> queryWrapper = new QueryWrapper<>();
        if (shop.getMerchantId() != null) {
            if (shop.getMerchantId()==0) {
                queryWrapper.isNull("merchant_id");
            }else {
                queryWrapper.eq("merchant_id", shop.getMerchantId());
            }
        }
        queryWrapper.eq("tenant_id", SecurityUtils.getDeptId());
        queryWrapper.orderByAsc("region");
        return list(queryWrapper);
    }


    @Override
    public List<ShopeeMerchant> handleShopList(List<ShopeeShop> shops) {
        List<ShopeeMerchant> merchants = new ArrayList<>();
        for (ShopeeShop shop : shops) {
            if (shop.getMerchantId() == null) {
                //本土店
                ShopeeMerchant merchant = new ShopeeMerchant();
                merchant.setShops(Arrays.asList(shop));
                merchant.setMerchantRegion(shop.getRegion());
                merchant.setMerchantName("独立店铺");
                merchants.add(merchant);
            } else {
                ShopeeMerchant merchant = null;
                for (ShopeeMerchant m : merchants) {
                    if (m.getMerchantId() == null) continue;
                    if (m.getMerchantId().longValue() == shop.getMerchantId().longValue()) merchant = m;
                }
                if (merchant == null) {
                    merchant = merchantService.getByMerchantId(shop.getMerchantId());
                    merchant.setShops(new ArrayList<ShopeeShop>());
                    merchants.add(merchant);
                }
                merchant.getShops().add(shop);
            }
        }
        return merchants;
    }

    /**
     * 获取下拉框的本土店铺
     *
     * @return
     */
    @Override
    public List<ShopeeShop> getShoplist(Long merchantId) {
        QueryWrapper<ShopeeShop> wrapper = new QueryWrapper<ShopeeShop>();
        if (merchantId == null) {
            wrapper.isNull("merchant_id");
        } else {
            wrapper.eq("merchant_id", merchantId);
        }
        wrapper.eq("tenant_id", SecurityUtils.getDeptId());
        return list(wrapper);
    }

}
